package salve;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.openymsg.network.FireEvent;
import org.openymsg.network.ServiceType;
import org.openymsg.network.YahooUser;
import org.openymsg.network.event.SessionEvent;
import org.openymsg.network.event.SessionFriendEvent;
import org.openymsg.network.event.SessionListEvent;
import org.openymsg.network.event.SessionListener;
import org.openymsg.network.event.SessionNotifyEvent;

import core.MessageBuilder;
import core.WriterThread;

public class YahooSessionListener implements SessionListener{

	private WriterThread writerThread;
	
	public YahooSessionListener(WriterThread writerThread) {
		this.writerThread = writerThread;
	}
	
	@Override
	public void dispatch(FireEvent event) {
		ServiceType serviceType = event.getType();
        System.out.println(event.toString());
        switch (serviceType) {
            case LIST:
                ProcessList(event);
                break;
            case Y6_STATUS_UPDATE:
                ProcessStatusUpdate(event);
                break;
            case NOTIFY:
                ProcessNotify(event);
                break;
            case MESSAGE:
                ProcessMessage(event);
                break;
        }
	}
	
	 /**
     * Lấy danh sách User
     * @param event
     */
    private void ProcessList(FireEvent event) {
        SessionListEvent sessionListEvent = (SessionListEvent) event.getEvent();
        Set<YahooUser> yahooUsers = sessionListEvent.getContacts();
        //ContactListType contactListType = sessionListEvent.getType();
        List<String> allGroups = new ArrayList<String>();
        List<String> allUsers = new ArrayList<String>();

        for (YahooUser yahooUser : yahooUsers) {
            int index = 0;
            for (String string : yahooUser.getGroupIds()) {
                if (!allGroups.contains(string)) {
                    allGroups.add(string);
                    allUsers.add(string);
                }
                index = allGroups.indexOf(string);
            }
            String str = allUsers.get(index) + ";" + yahooUser.getId();
            allUsers.set(index, str);
        }
        for (String string : allUsers) {
            writerThread.QueueMessage(MessageBuilder.BuildFriendList(string));
        }
    }

    /**
     * Những User Online và Status
     * @param event
     */
    private void ProcessStatusUpdate(FireEvent event) {
        SessionFriendEvent sessionFriendEvent = (SessionFriendEvent) event.getEvent();
        YahooUser yahooUser = sessionFriendEvent.getUser();
        for (String group : yahooUser.getGroupIds()) {
        	writerThread.QueueMessage(MessageBuilder.BuildUserStatus(group,yahooUser.getId(), yahooUser.getCustomStatus(), yahooUser.getCustomStatusMessage()));
		}        
    }

    private void ProcessNotify(FireEvent event) {
        SessionNotifyEvent sessionNotifyEvent = (SessionNotifyEvent) event.getEvent();
        sessionNotifyEvent.isTyping();
    }

    private void ProcessMessage(FireEvent event) {
        SessionEvent sessionEvent = (SessionEvent) event.getEvent();
        String message = MessageBuilder.BuildMessage(sessionEvent.getFrom(), sessionEvent.getMessage());
        writerThread.QueueMessage(message);
    }

}
